/**********************************************************************************************************************
 *  FILE DESCRIPTION
 *  -------------------------------------------------------------------------------------------------------------------
 *          File:  CCanTest.c
 *        Config:  S32K144_Start.dpa
 *     SW-C Type:  CCanTest
 *
 *     Generator:  MICROSAR RTE Generator Version 4.19.0
 *                 RTE Core Version 1.19.0
 *       License:  CBD1800257
 *
 *   Description:  C-Code implementation template for SW-C <CCanTest>
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of version logging area >>                DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

/* PRQA S 0777, 0779 EOF */ /* MD_MSR_5.1_777, MD_MSR_5.1_779 */
/* PRQA S 0857 EOF */ /* MD_MSR_1.1_857 */
/* PRQA S 0614 EOF */ /* MD_Rte_TestCode */

/* PRQA S 3109 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3112 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3197 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3198 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3199 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3201 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3203 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3205 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3206 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3218 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3229 EOF */ /* MD_Rte_TestCode */
/* PRQA S 2002 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3334 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3417 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3426 EOF */ /* MD_Rte_TestCode */
/* PRQA S 3453 EOF */ /* MD_Rte_TestCode */

/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of version logging area >>                  DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

#include "Rte_CCanTest.h" /* PRQA S 0857 */ /* MD_MSR_1.1_857 */

#define COM_GROUP_TEST
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of include and declaration area >>        DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

#include "string.h"
#include "Com.h"
#include "Adc.h"
#include "Pwm.h"
#include "Gpt.h"
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of include and declaration area >>          DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 *
 * Used AUTOSAR Data Types
 *
 **********************************************************************************************************************
 *
 * Primitive Types:
 * ================
 * boolean: Boolean (standard type)
 * uint8: Integer in interval [0...255] (standard type)
 *
 *********************************************************************************************************************/


#define CCanTest_START_SEC_CODE
#include "CCanTest_MemMap.h" /* PRQA S 5087 */ /* MD_MSR_19.1 */

/**********************************************************************************************************************
 *
 * Runnable Entity Name: CCanTest_InitRunnable
 *
 *---------------------------------------------------------------------------------------------------------------------
 *
 * Executed once after the RTE is started
 *
 *********************************************************************************************************************/
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of documentation area >>                  DO NOT CHANGE THIS COMMENT!
 * Symbol: CCanTest_InitRunnable_doc
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of documentation area >>                    DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

FUNC(void, CCanTest_CODE) CCanTest_InitRunnable(void) /* PRQA S 0850 */ /* MD_MSR_19.8 */
{
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of runnable implementation >>             DO NOT CHANGE THIS COMMENT!
 * Symbol: CCanTest_InitRunnable
 *********************************************************************************************************************/
#ifdef ADC_IRQ_TEST
  uint16 u6ResultDataBuffer[1]={0};

  Adc_SetupResultBuffer(AdcGroup_ADC0_SW_IRQ,u6ResultDataBuffer);
  Adc_EnableGroupNotification(AdcGroup_ADC0_SW_IRQ);
#endif

#ifdef GPT_TEST
  Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH0_ONESHOT);
  Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH1_CONTINUOUS);

  Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH0_ONESHOT,24000);
  Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH1_CONTINUOUS,24000);
#endif
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of runnable implementation >>               DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
}

/**********************************************************************************************************************
 *
 * Runnable Entity Name: CCan_MainFunction
 *
 *---------------------------------------------------------------------------------------------------------------------
 *
 * Executed if at least one of the following trigger conditions occurred:
 *   - triggered on TimingEvent every 200ms
 *
 **********************************************************************************************************************
 *
 * Output Interfaces:
 * ==================
 *   Explicit S/R API:
 *   -----------------
 *   Std_ReturnType Rte_Write_SendReceive_Send_sig_LampCnt_u8_Signal(uint8 data)
 *   Std_ReturnType Rte_Write_SendReceive_Send_sig_RearInteriorLight_Boolean_Signal(boolean data)
 *   Std_ReturnType Rte_Write_SendReceiver_Send_sig_Com_Signal_Tx_State_Manually_Boolean_Signal(boolean data)
 *
 *********************************************************************************************************************/
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of documentation area >>                  DO NOT CHANGE THIS COMMENT!
 * Symbol: CCan_MainFunction_doc
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of documentation area >>                    DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


#ifdef GPT_TEST
  static uint8 u8ComSendData = 0x88;
#endif
FUNC(void, CCanTest_CODE) CCan_MainFunction(void) /* PRQA S 0850 */ /* MD_MSR_19.8 */
{
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of runnable implementation >>             DO NOT CHANGE THIS COMMENT!
 * Symbol: CCan_MainFunction
 *********************************************************************************************************************/
  // uint8 u8PendingData = 1;
  // uint8 u8TriggeredData = 1;

  // static uint8 u8TriggeredTimer = 0;

  // Rte_Write_SendReceive_Send_sig_RearInteriorLight_Boolean_Signal(0x01);
  // Rte_Write_CCanTest_SendReceive_Send_sig_LampCnt_u8_Signal(0x12);

  // Rte_Write_CCanTest_SendReceiver_Send_sig_Com_Signal_Tx_State_Manually_Boolean_Signal(0x01);

  // Com_SendSignal(ComConf_ComSignal_sig_Com_Signal_Manually_Pending,&u8PendingData);

  // if(u8TriggeredTimer == 10)
  // {
  //   Com_SendSignal(ComConf_ComSignal_sig_Com_Signal_Manually_Triggered,&u8TriggeredData);
  //   u8TriggeredTimer = 0;
  // }
  // else
  // {
  //   u8TriggeredTimer++;
  // }
  #ifdef COM_GROUP_TEST
    static uint8 u8TriggeredKeyData = 0x01;
    static uint16 u16TriggeredValueData = 0x0000;

    if(u8TriggeredKeyData < 0xFF)
    {
      u8TriggeredKeyData++;
    }
    if(u16TriggeredValueData < 0xFFFF)
    {
      u16TriggeredValueData++;
    }

    Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
    Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
    Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
  #endif
  // static uint16 u16PwmData = 0x00;
  // static Pwm_OutputStateType pwmOutputState = 0;

  #ifdef ADC_TEST
    static uint16 u16AdcReadData = 0;
    Adc_StartGroupConversion(AdcGroup_ADC0_SW);
    Adc_ReadGroup(AdcGroup_ADC0_SW,&u16AdcReadData);
    u8ComSendData = u16AdcReadData/20;
    Com_SendSignal(ComConf_ComSignal_sig_LampCnt_omsg_MyECU_Lamp_oCAN00_f37e68ea_Tx,&u8ComSendData);
  #endif
  #ifdef GPT_TEST
    Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_LPIT0_CH0_ONESHOT,24000);
  #endif
  // Adc_StartGroupConversion(AdcGroup_ADC0_SW_IRQ);
  // u16PwmData = u16AdcReadData;
  // Pwm_SetDutyCycle(PwmConf_PwmChannelConfigSet_PwmChannel_FTM0_CH0,u16PwmData);
  // pwmOutputState = Pwm_GetOutputState(PwmConf_PwmChannelConfigSet_PwmChannel_FTM0_CH0);
  
  // if(pwmOutputState == PWM_HIGH)
  // {
  //   Adc_StopGroupConversion(AdcGroup_ADC0_SW);
  // }

  //Pwm_SetDutyCycle(PwmConf_PwmChannelConfigSet_PwmChannel_FTM0_CH0,0x4000);

  //Pwm_SetDutyCycle(PwmConf_PwmChannelConfigSet_PwmChannel_FTM0_CH0,0x8000);
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of runnable implementation >>               DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
}


#define CCanTest_STOP_SEC_CODE
#include "CCanTest_MemMap.h" /* PRQA S 5087 */ /* MD_MSR_19.1 */


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of function definition area >>            DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of function definition area >>              DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of removed code area >>                   DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of removed code area >>                     DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

/**********************************************************************************************************************
 MISRA 2004 violations and justifications
 *********************************************************************************************************************/

/* module specific MISRA deviations:
   MD_Rte_TestCode:
     Reason:     This justification is used within the generated test code by the Rte Analyzer.
     Risk:       No functional risk.
     Prevention: Not required.

*/
FUNC(void, COM_APPL_CODE) Com_CbkRxInvalidNotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x01;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(void, COM_APPL_CODE) Com_CbkRxNotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x02;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(void, COM_APPL_CODE) Com_CbkRxTimeoutNotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x03;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(boolean, COM_APPL_CODE) Com_CbkRxCallout(PduIdType PduId, P2CONST(PduInfoType, AUTOMATIC, COM_APPL_DATA) PduInfoPtr)
{
  return TRUE;
}


FUNC(void, COM_APPL_CODE) Com_CbkTxINotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x11;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(void, COM_APPL_CODE) Com_CbkTxTimeoutNotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x12;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(void, COM_APPL_CODE) Com_CbkTxErrorNotification(void)
{
#ifdef COM_ERROR_TEST
  u8TriggeredKeyData = 0x13;
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Key_Triggered_Manually,&u8TriggeredKeyData);
  Com_SendSignal(ComConf_ComGroupSignal_ComGroupSignal_Value_Triggered_Manually,&u16TriggeredValueData);
  Com_SendSignalGroup(ComConf_ComSignalGroup_ComSignalGroup_Triggered_Manually);
#endif
}

FUNC(boolean, COM_APPL_CODE) Com_CbkTxCallout(PduIdType PduId, P2VAR(PduInfoType, AUTOMATIC, COM_APPL_DATA) PduInfoPtr)
{
  return TRUE;
}


void AdcGroup_ADC0_Notification(void)
{
  #ifdef ADC_IRQ_TEST
    static uint16 u16AdcReadData_IRQ = 0;
    static uint8 u8ComSendData_IRQ = 0x88;

    Adc_ReadGroup(AdcGroup_ADC0_SW_IRQ,&u16AdcReadData_IRQ);
    Adc_DisableGroupNotification(AdcGroup_ADC0_SW_IRQ);
    u8ComSendData_IRQ = u16AdcReadData_IRQ/20;
    Com_SendSignal(ComConf_ComSignal_sig_LampCnt_omsg_MyECU_Lamp_oCAN00_f37e68ea_Tx,&u8ComSendData_IRQ);
  #endif
}

void GPT_Lpit0Cbk_CH0()
{
  #ifdef GPT_TEST
    u8ComSendData++;
  #endif
}

void GPT_Lpit0Cbk_CH1()
{
  #ifdef GPT_TEST
    u16TriggeredValueData++;
  #endif
}